package com.masterclient.app.modules.income.dealer;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.eoner.baselib.utils.status.StatusBarUtil;
import com.eoner.baselib.utils.toast.ToastUtil;
import com.eoner.managerlibrary.router.AroutePath;
import com.eoner.managerlibrary.user.UserDataManager;
import com.masterclient.app.BaseActivity;
import com.masterclient.app.R;
import com.masterclient.app.base.CashCommonDialog;
import com.masterclient.app.base.ConfirmDialog;
import com.masterclient.app.businessmodule.vipmodule.auth.UploadIDsActivity;
import com.masterclient.app.modules.income.contract.DealerBankCardManageContract;
import com.masterclient.app.modules.income.contract.DealerBankCardManagePresenter;
import com.masterclient.app.modules.income.dealer.model.DealerBankCardBean;
import com.masterclient.app.modules.income.adapter.ManageBankCardAdapter;
import com.masterclient.app.widget.TitlebarView;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import jp.wasabeef.recyclerview.animators.FlipInBottomXAnimator;

@Route(path = AroutePath.DEALER_BACK_CARD_MANAGE_ACTIVITY)
public class DealerBankCardManageActivity extends BaseActivity<DealerBankCardManagePresenter> implements DealerBankCardManageContract.View {

    @BindView(R.id.title_view)
    TitlebarView title_view;
    @BindView(R.id.recycler)
    RecyclerView recyclerView;
    @BindView(R.id.scrollView)
    NestedScrollView nestedScrollView;
    @BindView(R.id.view_empty)
    View emptyView;
    @BindView(R.id.view_add_bank_card)
    View viewAddBankCard;

    private ConfirmDialog confirmDialog;
    private CashCommonDialog ids_dialog;
    private ManageBankCardAdapter manageBankCardAdapter;
    private List<DealerBankCardBean> bankCardData;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dealer_bankcard_manage);
        ButterKnife.bind(this);
        StatusBarUtil.setStatusBarColor(getActivity(), Color.parseColor("#FFFFFF"));

        manageBankCardAdapter = new ManageBankCardAdapter(mContext);
        manageBankCardAdapter.SetOnDefaultSelectedListener(position -> {
            manageBankCardAdapter.notifyItemMoved(position,0);
            mPresenter.updateBankCardDefault(bankCardData.get(position).bankcardCertId);
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(manageBankCardAdapter);
        manageBankCardAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            if (view.getId() == R.id.view_unbind) {
                bankRemove(position);
            }
        });

        FlipInBottomXAnimator defaultItemAnimator = new FlipInBottomXAnimator();
        defaultItemAnimator.setAddDuration(1000);
        defaultItemAnimator.setRemoveDuration(1000);
        defaultItemAnimator.setChangeDuration(1000);
        defaultItemAnimator.setMoveDuration(1000);
        recyclerView.setItemAnimator(defaultItemAnimator);

        title_view.setOnViewClick(new TitlebarView.onViewClick() {
            @Override
            public void leftClick() {
                finish();
            }

            public void rightClick() {
            }
        });
        mPresenter.getBankCardInfo();
    }

    @OnClick({R.id.view_add_bank_card, R.id.view_add_bank_card2})
    void onClick(View v) {
        Intent intent;
        if (bankCardData != null && bankCardData.size() >= 10) {
            ToastUtil.showToast("最多绑定10张银行卡，绑定操作无效");
            return;
        }
        if (v.getId() == R.id.view_add_bank_card || v.getId() == R.id.view_add_bank_card2) {
            if (TextUtils.isEmpty(UserDataManager.getInstance().getIdCardNo())) {
                display_upload_ids_dialog();
            } else {
                intent = new Intent(DealerBankCardManageActivity.this, DealerBankCardInsertActivity.class);
                startActivityForResult(intent, REFRESH_CARD);
            }
        }
    }

    public static final int REFRESH_CARD = 2;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case REFRESH_CARD:
                mPresenter.getBankCardInfo();
                break;
            case 104:

                break;
        }
    }

    private void bankRemove(int position) {
        confirmDialog = new ConfirmDialog(mContext, "是否解绑当前银行卡？", "暂不解绑", "立即解绑");
        confirmDialog.setOnChooseListerner(new ConfirmDialog.OnChooseListerner() {
            @Override
            public void cancel() {
                confirmDialog.dismiss();
            }

            @Override
            public void confirm() {
                mPresenter.bankRemove(bankCardData.get(position).bankcardCertId);
                confirmDialog.dismiss();
            }
        });
        confirmDialog.show();
    }

    private void display_upload_ids_dialog() {
        if (ids_dialog == null) {
            ids_dialog = new CashCommonDialog(mContext, "为保证您的资金账户安全,请您先绑定本人身份证信息再进行绑卡", "取消", "去实名", 1);
        }
        ids_dialog.setOnChooseListerner(new CashCommonDialog.OnChooseListerner() {
            @Override
            public void cancel() {
                ids_dialog.dismiss();
            }

            @Override
            public void confirm() {
                ids_dialog.dismiss();
                Intent intent = new Intent(DealerBankCardManageActivity.this, UploadIDsActivity.class);
                startActivity(intent);
            }
        });
        ids_dialog.show();
    }

    /**
     * 设置页面置空状态
     *
     * @param status status == 1：数据返回为空
     */
    private void setEmptyStatus(int status) {
        if (status == 1) {
            emptyView.setVisibility(View.VISIBLE);
            viewAddBankCard.setVisibility(View.GONE);
        } else {
            viewAddBankCard.setVisibility(View.VISIBLE);
            emptyView.setVisibility(View.GONE);
        }
    }

    @Override
    public void getBankSuccess(List<DealerBankCardBean> data) {
        if (data != null && data.size() > 0) {
            bankCardData = data;
            setEmptyStatus(0);
            manageBankCardAdapter.setNewData(data);
            nestedScrollView.fling(0);
            nestedScrollView.smoothScrollTo(0, 0);
        } else {
            setEmptyStatus(1);
        }
    }

    @Override
    public void getBankCardFail(String msg) {
        setEmptyStatus(1);
        ToastUtil.showToast(msg);
    }

    @Override
    public void BankRemoveSuccess() {
        mPresenter.getBankCardInfo();
    }

    @Override
    public void BankRemoveFailed(String msg) {
        ToastUtil.showToast(msg);
    }

    @Override
    public void updateBankCardDefaultSuccess() {
        mPresenter.getBankCardInfo();
    }

    @Override
    public void updateBankCardDefaultFailed(String msg) {
        ToastUtil.showToast(msg);
    }
}
